#include"SLNode.h"


void SLNPrint(SLNode* phead)
{
	SLNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL");
}

SLNode* SLNCreat(Type x)
{
	
		SLNode* Newnode = (SLNode*)malloc(sizeof(SLNode));
		if (Newnode == NULL)
		{
			printf("节点申请失败");
			exit(-1);
		}
		Newnode->next = NULL;
		Newnode->data = x;

		return Newnode;
	

}

void SLNPushBack(SLNode** pphead, Type x)//二级指针 
{

	SLNode* Newnode = SLNCreat(x);
	
	
	if (*pphead == NULL)//对指针的地址解引用 得到指针变量
	{
		*pphead = Newnode;
	}
	else
	{
		SLNode* cur = *pphead;
		while (cur->next != NULL)
			cur = cur->next;

		cur->next = Newnode;
	}
}

void SLNPopBack(SLNode** pphead)
{
	SLNode** cur = pphead;
	if (*cur == NULL)
		return;
	else if((*cur)->next==NULL)//*pphead要打括号
	{
		free(*cur);
		*cur = NULL;
	}
	else
	{
		SLNode* pre=NULL;
		SLNode* tail = *pphead;
		while (tail->next!= NULL)
		{
			pre = tail;
			tail = tail->next;	    
		}
		free(tail);
		pre->next = NULL;
	}
	
}

void SLNodePushFront(SLNode** pphead, Type x)
{
	SLNode* Newnode = SLNCreat(x);
	Newnode->next = *pphead;
	*pphead = Newnode;
}

void SLNodePopFront(SLNode** pphead)
{
	if ((*pphead)== NULL)
		return;
	else
	{
		SLNode* next= NULL;
		next = (*pphead)->next;
		free(*pphead);
		*pphead = NULL;
		*pphead = next;
	}
}

void SLNodeInsertAfter(SLNode** pos, Type x)
{
	assert(pos);
	SLNode* Newnode =SLNCreat(x);
	Newnode->next = (*pos)->next;
	 (*pos)->next = Newnode; 
}


void SLNodeEraseAfter(SLNode** pos) 
{
	assert(pos);
	if (*pos == NULL || (*pos)->next == NULL) 
		return; 
	

	SLNode* toDelete = (*pos)->next; 
	(*pos)->next = toDelete->next;   
	free(toDelete);                 
}
	